<?php

namespace app\api\controller;

use app\common\model\NoticeLeave;
use app\common\model\NoticeMessage;
use app\common\model\QuestionType;
use app\common\model\Question;
use think\Db;
use think\Exception;
use think\Log;
use think\Request;
use think\Validate;

class Notice extends User
{

    /**
     * 通知消息列表
     */
    public function messages(Request $request)
    {
        $map  = [];
        $rows = NoticeMessage::where($map)->field('id,title,describe,path,content,create_time')->paginate(5);
        $rows = ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
        api_return(1,'获取成功',$rows);
    }

    /**
     * 通知消息详情
     */
    public function message_details(Request $request)
    {
        $id = $request->param('id');

        $data = NoticeMessage::find($id);
        
        api_return(1,'获取成功',$data);
    }

    /**
     * 留言列表
     */
    public function leaves()
    {
        $rows = NoticeLeave::alias('a')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where('a.user_id',$this->user_id)
            ->order('id')
            ->field('u.header_img,a.*')
            ->select();
        foreach ($rows as $row)
        {
            if($row['status'] ==2)
                $row['header_img'] = '';
        }
        api_return(1,'获取成功',$rows);
    }

    /**
     * 通知留言
     */
    public function leave(Request $request)
    {
        $request = $request->param();
        $validate = new Validate([
            'type' => 'require|in:1,2',
            'content' => 'require|max:250',
        ]);
        if (!$validate->check($request)) {
            get_error_msg($validate->getError());
        }

        NoticeLeave::create([
            'content' => $request['content'],
            'type'    => $request['type'],
            'user_id' => $this->user_id,
        ]);
        api_return(1,'留言成功');
    }

    /**
     * 问题列表
     */
    public function questions()
    {
        $rows = QuestionType::with('question')->where('status',1)->order('sort')->select();
        api_return(1,'获取成功',$rows);
    }

    /**
     * 问题详情
     */
    public function question(Request $request)
    {
        $id= $request->param('id');
        $data = Question::find($id);

        api_return(1,'获取成功', $data);
    }

    /**
     * 热门问题
     */
    public function hotQuestions()
    {
        $rows = Question::where(['status'=> 1,'is_hot' => 1])->select();
        api_return(1,'获取成功',$rows);
    }

    /**
     * 玩转哈尼 问题列表
     */
    public function funQuestions()
    {
        $rows = Question::hasWhere('questionType',['title' => '玩转miyo'])->select();
        api_return(1,'获取成功',$rows);
    }
}